package mygang.model.constructor.field;

import java.io.Serializable;
import javax.persistence.*;
import mygang.helper.Utils;
import mygang.model.BaseModel;
import mygang.model.Option;
import mygang.model.OptionInterface;
import mygang.model.constructor.EntityModel;
import mygang.model.constructor.TitleInterface;
import org.hibernate.annotations.Proxy;

/**
 *
 * @author Volodymyr Lavrynovych <volodya.lavrynovych@gmail.com>
 * date: 22/09/2012
 */
@Entity
@Table(name = "FIELD")
@Proxy(lazy = false)
public class Field extends BaseModel implements Serializable, TitleInterface, OptionInterface {

    @Id
    @Column(name = "FIELD_ID")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    
    @ManyToOne(cascade=CascadeType.PERSIST)
    @JoinColumn(name="FT_ID")
    private FieldTemplate template;
    
    @Column(name = "FIELD_TITLE")
    private String title;
    
    @Column(name = "FIELD_DESCRIPTION")
    private String description;
    
    @Column(name = "FIELD_COLUMN_NAME")
    private String columnName;
    
    @Column(name = "FIELD_IS_ACTIVE")
    private boolean isActive;
    
    @Column(name = "FIELD_IS_ATTRIBUTE")
    private boolean isAttribute;
       
    @ManyToOne(cascade=CascadeType.PERSIST, fetch = FetchType.EAGER)
    @JoinColumn(name = "ENTITY_ID")
    private EntityModel entity;
    
    @Transient
    private String value;
    
    @Transient
    private long entityId;
    
    public Field() {
    }
    
    public Field(long id) {
        this.id = id;
    }

    public Field(long id, FieldTemplate template, String title, String description, String columnName, boolean isActive, boolean isAttribute) {
        this(template, title, description, columnName, isActive, isAttribute);
        this.id = id;
    }

    public Field(FieldTemplate template, String title, String description, String columnName, boolean isActive, boolean isAttribute) {
        this.template = template;
        this.title = title;
        this.description = description;
        this.columnName = columnName;
        this.isActive = isActive;
        this.isAttribute = isAttribute;
    }
    
    public Field(long id, String title, FieldTemplate template) {
        this(title, template);
        this.id = id;
    }
    
    public Field(String title, FieldTemplate template) {
        this.title = title;
        this.template = template;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public FieldTemplate getTemplate() {
        return template;
    }

    public void setTemplate(FieldTemplate template) {
        this.template = template;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Option toOption() {
        return new Option(getId(), getTitle());
    }

    public String getColumnName() {
        return columnName;
    }

    public void setColumnName(String columnName) {
        this.columnName = columnName;
    }

    public boolean isActive() {
        return isActive;
    }

    public void setActive(boolean isActive) {
        this.isActive = isActive;
    }

    public boolean isAttribute() {
        return isAttribute;
    }

    public void setAttribute(boolean isAttribute) {
        this.isAttribute = isAttribute;
    }

    public EntityModel getEntity() {
        return entity;
    }

    public void setEntity(EntityModel entity) {
        this.entity = entity;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public long getEntityId() {
        return entityId;
    }

    public void setEntityId(long entityId) {
        this.entityId = entityId;
    }
    
    @Override
    public String toString(){
        return Utils.toString(this);
    }
}